package main

import (
	"fmt"
	"gopkg.in/ini.v1"
	"os"
)

func checkErr(err error) {
	if err != nil {
		panic(err.Error())
	}
}
func saveinfo() {
	cfg := ini.Empty()
	defaultSelection := cfg.Section("")
	defaultSelection.Comment = "默认配置信息"
	defaultSelection.NewKey("app_name", "api-rpc-pay")
	defaultSelection.NewKey("host", "127.0.0.1")
	defaultSelection.NewKey("port", "8080")

	mysqlMasterSelection, err := cfg.NewSection("mysql-master")
	checkErr(err)
	mysqlMasterSelection.NewKey("host", "127.0.0.1")
	mysqlMasterSelection.NewKey("port", "3306")
	mysqlMasterSelection.NewKey("type", "mysql")
	mysqlMasterSelection.NewKey("name", "jdk_pay")
	mysqlMasterSelection.NewKey("user", "root")
	mysqlMasterSelection.NewKey("pwd", "sin")
	mysqlMasterSelection.NewKey("charset", "utf8mb4")
	mysqlMasterSelection.NewKey("prefix", "kc_")
	mysqlMasterSelection.NewKey("maxidle", "50")
	mysqlMasterSelection.NewKey("maxconn", "100")
	mysqlMasterSelection.NewKey("maxlifetime", "12600")
	cfg.SaveTo("app.ini")
	cfg.WriteTo(os.Stdout)

}
func defalutSelectionIni() {
	cfg := ini.Empty()
	defaultSection := cfg.Section("")
	//变量使用小驼峰的方式
	defaultSection.NewKey("app_name", "sin")
	defaultSection.NewKey("log_level", "DEBUG")

	mysqlSection, err := cfg.NewSection("mysql")
	if err != nil {
		fmt.Println("new mysql selection failed:", err)
		return
	}
	mysqlSection.NewKey("ip", "127.0.0.1")
	mysqlSection.NewKey("port", "3306")
	mysqlSection.NewKey("user", "root")
	mysqlSection.NewKey("password", "sin")
	mysqlSection.NewKey("database", "sin")

	redisSelection, err := cfg.NewSection("redis")
	if err != nil {
		fmt.Println("new redis selection failed:", err)
		return
	}
	redisSelection.Comment = "redis master配置"
	redisSelection.NewKey("ip", "127.0.0.1")
	redisSelection.NewKey("port", "6458")

	err = cfg.SaveTo("my.ini")

	if err != nil {
		fmt.Println("SaveTo failed:", err)
	}

	err = cfg.SaveToIndent("my-prettry.ini", "\t")

	if err != nil {
		fmt.Println("SaveToIndent failed:", err)
	}

	cfg.WriteTo(os.Stdout)

	fmt.Println()

	cfg.WriteToIndent(os.Stdout, "\t")
}

//保存信息
// struct->ini->savefile
//ini-io-reader->out struct
//new ini.empty
//selection
// ini.newSlectoopn
//host        = 127.0.0.1
//port        = 3306
//type        = mysql
//name        = jdk_pay
//user        = root
//pwd         = sin
//charset     = utf8mb4
//prefix      = kc_
//maxidle     = 50
//maxconn     = 100
//maxlifetime = 12600
type AppConfig struct {
	AppName     string            `ini:"app_name"`
	Host        string            `ini:"host"`
	Port        int               `ini:"port"`
	MySQLMaster MySQLMasterConfig `ini:"mysql-master"`
}
type MySQLMasterConfig struct {
	Host        string `ini:"host"`
	Port        int    `ini:"port"`
	Type        string `ini:"type"`
	Name        string `ini:"name"`
	User        string `ini:"user"`
	Pwd         string `ini:"pwd"`
	Charset     string `ini:"charset"`
	Prefix      string `ini:"prefix"`
	Maxidle     int    `ini:"maxidle"`
	Maxconn     int    `ini:"maxconn"`
	Maxlifetime int    `ini:"maxlifetime"`
}

func main() {
	cfg, _ := ini.Load("app.ini")
	app := &AppConfig{}
	cfg.MapTo(app)
	fmt.Printf("%v", app)

	newcfg := ini.Empty()
	newcfg.ReflectFrom(app)
	newcfg.SaveTo("sin.ini")

}
